var form
layui.use('form', function () {
    form = layui.form;
    var id = getParam("id");
    var type = getParam("type");
    dict2Select("dimension","dimension");
    if (id != null && typeof (id) != "undefined" && id != "undefined"&& id!="null") {
        io.get('/mm/rules/' + id,function (result) {
                    var data = result.data;
                    $("#ruleNo").attr("readOnly",true);
                    form.val('editForm',data);
                    var field = data.fieldList;
                    addTr(field);
                    form.render();
            });
    }else{
        $("#version").val(1);
    }
    if(type==1){
        $("#editForm input").attr("readOnly",true);
        $("#editForm select").attr("disabled",true);
        $("#editForm textarea").attr("readOnly",true);
        $("#editForm input[type='checkbox']").prop('disabled', true);
        $("#buttondiv").hide();
        $("#btnSave").hide();
    }
    form.render();
    
  //监听提交
    form.on('submit(checkExp)', function (data) {
        var cnName = {};
        var fieldList = new Array();
        var returnFlag = false;
        $("input[name='fieldId']").each(function (index, item) {
            var name=$.trim($(this).attr('data_val'));
            var id=$(this).attr('id');
            if(cnName[name] == null){
                cnName[name]=id;
            }else if(id!=cnName[name]){
                layer.alert("字段"+id+"与字段"+cnName[name]+"内部中文名重复",{icon:2})
                returnFlag = true;
            }
            fieldList.push($(this).attr('id'));
        });
        if(returnFlag){
            return ;
        }
        data.field.fieldList = fieldList;
        io.post('/engine/expression/check',JSON.stringify(data.field),
            function (result) {
	        	layer.msg("校验通过",{icon:1});
            },false);
        return false;
    });
    
    //监听提交
    form.on('submit(save)', function (data) {
        var cnName = new Map();
        var fieldList = new Array();
        var returnFlag = false;
        $("input[name='fieldId']").each(function (index, item) {
            var name=$.trim($(this).attr('data_val'));
            var id=$(this).attr('id');
            if(cnName[name] == null){
                cnName[name]=id;
            }else if(id!=cnName[name]){
                layer.alert("字段"+id+"与字段"+cnName[name]+"内部中文名重复",{icon:2})
                returnFlag = true ;
            }
            fieldList.push(id);
        });
        if(returnFlag){
            return ;
        }
        data.field.fieldList = fieldList;
        if(data.field.id) {
            io.get('/mm/rule-relas/' + data.field.id, function (result) {
                var value = result.data;
                if (value) {
                    layer.confirm('该规则已在模型：' + value + '中使用，确认修改保存吗？', {icon: 3, title: '确认提示'}, function (index) {
                        submitFunc(data.field);
                    });
                } else {
                    submitFunc(data.field);
                }
            });
        }else{
            submitFunc(data.field);
        }
        return false;
    });
    layui.$('#add').on('click', function(){
        layer.open({
            type: 2,
            title: '添加字段',
            area: ['1250px', '700px'],
            content: 'addField.html?',
        });
    });
    layui.$('#delete').on('click',  function(formData){
        var length = $("input[name='fieldId']:checked").length;
        if(length==0){
            layer.alert("请选择要删除的字段",{icon:2});
            return ;
        }
        $("input[name='fieldId']:checked").each(function (index, item) {
            var $btn = $(this);
            var $row = $btn.closest("tr");
            $row.remove();
            var id = Number($(this).attr('id'));
            var ind=$.inArray(id,dataArrAll);
            dataArrAll.splice(ind,1);
        });
        return false;
    });
    form.verify({
        number: [
            /^[0-9]\d*$/,'请输入整数'
        ],
    double: [
        /^[0-9]*([.][0-9]+)?$/,'请输入数字'
    ]
    });
});
var dataArrAll = new Array();
function addTr(dataArr) {
    for(var i=0;i<dataArr.length;i++)
    {
        var item = dataArr[i];
        if($.inArray(item.id, dataArrAll)==-1) {
            var buttons = "<input type='checkbox' name='fieldId' id='" + item.id + "' data_val='"+item.innerCName+"' lay-skin='primary'>";
            var $row = $("<tr><td>" + buttons + "</td><td style='text-align: left'>" + item.id + "</td><td style='text-align: left'>" + item.innerCName + "</td><td style='text-align: left'>"
                + item.innerEName + "</td><td style='text-align: left'>" + translate("fieldType", item.innerType) + "</td><td style='text-align: left'>" + item.description + "</td></tr>");
            $("#fieldtb").find("tbody").append($row);
            dataArrAll.push(item.id);
        }
    }
    form.render();
}

function submitFunc(data) {
    io.post('/engine/expression/check',JSON.stringify(data),
        function (result) {
            io.post('/mm/rules',JSON.stringify(data),
                function (result) {
                    closePage();
                    //刷新列表
                    window.parent.refreshTableData();

                });
        });
}